package com.fang.service;

import java.util.List;

import com.fang.dao.CartDao;
import com.fang.dao.GoodsDao;
import com.fang.entity.Cart;
import com.fang.entity.Goods;
import com.google.gson.Gson;

public class CartService {
	CartDao dao = new CartDao();
	GoodsDao gdao = new GoodsDao();
	public void addcart(Cart cart) {
//		查询购物商品记录
		Cart record = dao.query(cart);
		if(record==null){
			dao.addcart(cart);
		}else{
			Integer buynum = record.getBuynum()+cart.getBuynum();
			cart.setBuynum(buynum);
			dao.updatecart(cart);
		}
		
	}

	public List<Cart> querybyuid(int uid) {
	List<Cart> clist = dao.querybyuid(uid);
	for (Cart cart2 : clist) {
		if(cart2.getBuynum()==0)dao.delete(cart2);
		Goods goods = new Goods();
		goods.setId(cart2.getGid());
		List<Goods> querygoods = gdao.querygoods(goods);
		cart2.setGoods(querygoods.get(0));
	}
		return clist;
	}

	public void delete(Cart cart) {
		dao.delete(cart);
		
	}

	public void changnum(Cart cart) {
		
			
			dao.updatecart(cart);
			
			return ;
	}


}
